var util = require('util');
var config = require('./config.js');


// 获得MYSQL配置
config.mysql.info = function() {
    var mysql = config.mysql;
    return util.format('%s %d %s %s %s', mysql.ip, mysql.port, mysql.user, mysql.password, mysql.charset);
}

// 获取合服mongo地址
config.merge.mongo_address = function() {
    var mongo = config.merge.mongo;
    return util.format('%s:%d', mongo.ip, mongo.port);
}



// 获得某模块的服务器所在主机,attr=[id|ip|obj]
config.server.query = function(modName, sid, attr) {
    sid = +sid;
    attr = attr || 'id';
    var hosts = config.server.host;
    for (id in hosts) {
        var host = hosts[id];
        var sections = host[modName];
        if (!sections) continue;

        for (var i = 0; i < sections.length; ++i) {
            var section = sections[i];
            if (sid >= section[0] && sid <= section[1]) {
                if ('id' == attr) {
                    return id;
                } else if ('this' == attr) {
                    host.id = id;
                    return host;
                } else {
                    return host[attr];
                }
            }
        }
    }
}

// 获得某模块的主机列表attr=[id|ip]
config.server.list = function(modName, attr) {
    attr = attr || 'id';
    var values = [];
    var hosts = config.server.host;
    for (var id in hosts) {
        var host = hosts[id];
        if (!host[modName]) continue;

        if ('id' == attr) {
            values.push(id);
        } else {
            value = host[attr];
            if (value) {
                values.push(value);
            }
        }
    }
    
    return values;
}

// 获得所有主机列表,attr=[id|ip]
config.server.all = function(attr) {
    attr = attr || 'id';
    var hosts = config.server.host;
    var values = [];
    for (var id in hosts) {
        var host = hosts[id];
        if ('id' == attr) {
            values.push(id);
        } else {
            value = host[attr];
            if (value) {
                values.push(value);
            }
        }
    }
    return values;
}

// 获得某模块的主机的服务器列表
config.server.sids = function(modName, id) {
    var host = config.server.host[id];
    if (!host) return;

    var sections=host[modName];
    if (!sections) return;

    var sids = [];
    for (var i = 0; i < sections.length; ++i) {
        var section = sections[i];
        for (var sid = section[0]; sid <= section[1]; ++sid) {
            sids.push(sid);
        }
    }

    return sids;
}

// 获得某模块的服务器数据库地址
config.server.db_address = function(modName, sid) {
    var mod = config[modName];
    if (!mod || !mod.db_host_id) {
        return;
    }

    var hostId = mod.db_host_id(sid);
    if (!hostId) {
        return;
    }

    var host = config.server.host[hostId];
    if (!host || !host.mongo) {
        return;
    }

    return util.format('%s:%d', host.ip, host.mongo.port);
}



config.game.osid = function(sid) {
    return (+sid) + config.game.offset;
}

config.game.sidFromOsid = function(osid) {
    return (+osid) - config.game.offset;
}

// 游戏服的工作目录
config.game.path = function(sid) {
    sid = +sid;
    return util.format('online/%s%d/ssanguo', config.game.path_prefix, config.game.osid(sid));
}

// 游戏服的数据库名称
config.game.db_name = function(sid, merged) {
    sid = +sid;
    merged = +merged;
    return util.format('%s%s%d', config.game.db_prefix, merged ? '_' : '', config.game.osid(sid));
}



// 云服的工作目录
config.cloud.path = function(sid) {
    sid = +sid;
    return util.format('online/%s%d/ssanguo', config.cloud.path_prefix, sid);
}

// 云服的数据库名称
config.cloud.db_name = function(sid) {
    sid = +sid;
    return util.format('%s%d', config.cloud.db_prefix, sid);
}



// 跨服排行服的工作目录
config.cloud_rank.path = function(sid) {
    sid = +sid;
    return util.format('online/%s%d/ssanguo', config.cloud_rank.path_prefix, sid);
}

// 跨服排行服的数据库名称
config.cloud_rank.db_name = function(sid) {
    sid = +sid;
    return util.format('%s%d', config.cloud_rank.db_prefix, sid);
}



// 跨服金矿服的工作目录
config.cloud_mine.path = function(sid) {
    sid = +sid;
    return util.format('online/%s%d/ssanguo', config.cloud_mine.path_prefix, sid);
}

// 跨服金矿服的数据库名称
config.cloud_mine.db_name = function(sid) {
    sid = +sid;
    return util.format('%s%d', config.cloud_mine.db_prefix, sid);
}



// 决战天下服的工作目录
config.cloud_battle.path = function(sid) {
    sid = +sid;
    return util.format('online/%s%d/ssanguo', config.cloud_battle.path_prefix, sid);
}

// 决战天下服的数据库名称
config.cloud_battle.db_name = function(sid) {
    sid = +sid;
    return util.format('%s%d',config.cloud_battle.db_prefix, sid);
}



// 王者战场服的工作目录
config.champsbattle.path = function(sid) {
    sid = +sid;
    return util.format('online/%s%d/ssanguo', config.champsbattle.path_prefix, sid);
}

// 王者战场服的数据库名称
config.champsbattle.db_name = function(sid) {
    sid = +sid;
    return util.format('%s%d',config.champsbattle.db_prefix, sid);
}



// 群雄争霸服的工作目录
config.universe.path = function(sid) {
    sid = +sid;
    return util.format('online/%s%d/ssanguo', config.universe.path_prefix, sid);
}

// 群雄争霸服的数据库名称
config.universe.db_name = function(sid) {
    sid = +sid;
    return util.format('%s%d', config.universe.db_prefix, sid);
}


config.checkCloud = function() {
    var cloudIP = config.server.query('cloud', 1, 'ip');
    var cloudPort = config.cloud.port(1);
    var cloudOpened = (!cloudIP || !cloudPort) ? false : true;
    if (cloudOpened) {
       console.log('cloud        :[Y] is open');
    } else {
       console.log('cloud        :[X] not open');
    }

    var cloudMineIP = config.server.query('cloud_mine', 1, 'ip');
    var cloudMinePort = config.cloud_mine.port(1);
    if (!cloudMineIP || !cloudMinePort) {
       console.log('cloud_mine   :[X] not open');
    } else {
        if (cloudIP == cloudMineIP && cloudPort == cloudMinePort) {
            console.log('cloud_mine   :[?] in cloud');
        } else {
            console.log('cloud_mine   :[Y] is open');
        }
    }

    var cloudRankIP = config.server.query('cloud_rank', 1, 'ip');
    var cloudRankPort = config.cloud_rank.port(1);
    if (!cloudRankIP || !cloudRankPort) {
       console.log('cloud_rank   :[X] not open');
    } else {
        if (cloudIP == cloudRankIP && cloudPort == cloudRankPort) {
            console.log('cloud_rank   :[?] in cloud');
        } else {
            console.log('cloud_rank   :[Y] is open');
        }
    }

    var cloudBattleSid = config.game.cloud_battle_sid(1);
    if (cloudBattleSid) {
       console.log('cloud_battle :[Y] is open');
    } else {
       console.log('cloud_battle :[X] not open');
    }

    var champsbattleSid = config.game.champsbattle_sid(1);
    if (champsbattleSid) {
       console.log('champsbattle :[Y] is open');
    } else {
       console.log('champsbattle :[X] not open');
    }

    var cloudarenaSid = config.game.cloudarena_sid(1);
    if (cloudarenaSid) {
       console.log('cloudarena   :[Y] is open');
    } else {
       console.log('cloudarena   :[X] not open');
    }

    var universeSid = config.game.universe_sid(1);
    var universeIP = config.server.query('universe', universeSid, 'ip');
    if (universeSid && universeIP) {
       console.log('universe     :[Y] is open');
    } else {
       console.log('universe     :[X] not open');
    }

}

exports.config = config;
